/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/address/tmp.html', 'layer', 'address'], function (page, layer, address) {

    var render = template.compile(page);

    var apiList = {
        getAddressList: api + 'user/address',
        deleteAddress: api + 'user/address'
    };

    var symbol = {};

    var vm = new Page.creat({
        $id: 'address',
        $model: {
            data: {},
            $: {},
            orderInfo: null,
            select: function () {
                vm.$init({
                    setTitle: '选择收货地址'
                });
                vm.orderInfo = db.get('confirmOrder');
                if (vm.orderInfo) {
                    ex.jsonp({
                        url: apiList.getAddressList,
                        success: function (obj) {
                            if (!vm.isThisPage())return;
                            vm.data = {
                                list: obj.data,
                                Pares_address: address.getAddress,
                                select: "true"
                            };

                            $('#page').html(render(vm.data));

                            ex.async(function () {

                                vm.bindEvent(vm.event);
                                vm.bind$(vm.$);

                            });

                        }
                    })
                } else {
                    //todo 跳到错误页
                    throw 'Can\'t get order info';
                }

            },
            view: function () {
                vm.$init({
                    setTitle: '我的收货地址'
                });
                vm.orderInfo = null;
                ex.jsonp({
                    url: apiList.getAddressList,
                    success: function (obj) {
                        if (!vm.isThisPage())return;
                        console.log(obj);

                        vm.data = {
                            list: obj.data,
                            Pares_address: address.getAddress
                        };

                        $('#page').html(render(vm.data));

                        ex.async(function () {

                            vm.bindEvent(vm.event);
                            vm.bind$(vm.$);

                        });

                    }
                })
            },
            event: {
                toggleEditMode: function () {
                    if (symbol.EditMode) {
                        symbol.EditMode = false;
                        $('.slider').css('right', 0);
                        vm.$.topbtn.text('编辑');
                    } else {
                        symbol.EditMode = true;
                        $('.slider').css('right', 60);
                        vm.$.topbtn.text('完成');
                    }
                },
                editAddress: function (index) {
                    var thisAddress = vm.data.list[index];
                    db.set('Address' + thisAddress._id, thisAddress);
                    location.hash = '#/editAddress/' + thisAddress._id;
                },
                CheckAndSelectAddress: function (index) {
                    if (vm.orderInfo == null)return;
                    vm.orderInfo.address = vm.data.list[index];
                    db.set('confirmOrder', vm.orderInfo);
                    history.back();
                },
                deleteAddress: function (index) {
                    var thisAddress = vm.data.list[index];
                    var $this = $(this);
                    ex.jsonp({
                        url: apiList.deleteAddress,
                        method: 'DELETE',
                        data: {
                            _id: thisAddress._id
                        },
                        success: function (obj) {
                            if (!vm.isThisPage())return;
                            if (obj.code == 0) {
                                $this.parents('.address').remove();
                            } else {
                                layer.msg(obj.msg);
                            }
                        }
                    })
                }
            }
        }
    });


    return {
        select: function () {

            vm.select();
        },
        view: function () {

            vm.view();
        }
    }
});